<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--设备类型数据-->
      <el-col :span="spanColLeft" :xs="24" v-show="showLeft">
        <div class="head-container" style="margin-bottom: 10px">
          <span><i class="el-icon-collection"></i> 设备类型树</span>
        </div>
        <div class="head-container">
          <el-input
            v-model="drtypename"
            placeholder="请输入设备类型名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deviceTypeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--设备信息数据-->
      <el-col :span="spanColLeft" :xs="24" v-show="showLeft">
        <div class="head-container" style="margin-bottom: 10px">
          <span><i class="el-icon-key"></i> 设备列表</span>
        </div>
        <div class="head-container">
          <el-table
            v-loading="subLoading" :data="deviceLists" :height="tableHeight" highlight-current-row @row-click="handleDeviceRegList" style="width: 100%">
            <el-table-column label="序号" width="50" type="index"></el-table-column>
            <el-table-column label="设备名称" align="center" :show-overflow-tooltip="true" prop="drname"></el-table-column>
          </el-table>
        </div>
      </el-col>
      <!--设备属性数据-->
      <el-col :span="spanColRight" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="设备名称" prop="drName">
            <el-input
              v-model="queryParams.drName"
              clearable
              id="tagName"
              disabled
            />
          </el-form-item>
          <el-form-item label="变量标识" prop="tagName">
            <el-input
              v-model="queryParams.tagName"
              placeholder="请输入寄存器变量标识"
              clearable
              @keyup.enter.native="handleQuery"
              id="tagName"
            />
          </el-form-item>
          <el-form-item label="变量名称" prop="regName">
            <el-input
              v-model="queryParams.regName"
              placeholder="请输入变量名称（中文）"
              clearable
              @keyup.enter.native="handleQuery"
              id="regName"
            />
          </el-form-item>
          <el-form-item label="英文名称" prop="regNameen">
            <el-input
              v-model="queryParams.regNameen"
              placeholder="请输入变量名称（英文）"
              clearable
              @keyup.enter.native="handleQuery"
              id="regNameen"
            />
          </el-form-item>
          <el-form-item label="单位" prop="regUnits">
            <el-input
              v-model="queryParams.regUnits"
              placeholder="请输入单位"
              clearable
              @keyup.enter.native="handleQuery"
              id="regUnits"
            />
          </el-form-item>
          <el-form-item label="数据回显" prop="regSub">
            <el-select v-model="queryParams.regSub" placeholder="请选择数据回显" id="regSub" @change="handleQuery" clearable>
              <el-option
                v-for="item in subOptions"
                :key="item.subid"
                :label="item.subname"
                :value="String(item.subid)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="值类型" prop="regType">
            <el-select v-model="queryParams.regType" placeholder="请选择变量值类型" id="regType" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_value_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="读写类型" prop="regReadwrite">
            <el-select v-model="queryParams.regReadwrite" placeholder="请选择读写类型" id="regReadwrite" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_read_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="保存周期" prop="tagTime">
            <el-select v-model="queryParams.tagTime" placeholder="请选择保存周期" id="tagTime" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_data_save_time"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="变量分组" prop="regGroupId">
            <el-select v-model="queryParams.regGroupId" placeholder="变量分组" id="regGroupId" @change="handleQuery" clearable>
              <el-option
                v-for="group in groupOptions"
                :key="group.id"
                :label="group.groupName"
                :value="group.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="点位类型" prop="regDrshowtype">
            <el-select v-model="queryParams.regDrshowtype" placeholder="请选择点位类型" id="regDrshowtype" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_reg_dr_show_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="3D展示" prop="isThreedShow">
            <el-select v-model="queryParams.isThreedShow" placeholder="请选择是否3D展示" id="isThreedShow" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_is_show"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="历史存储" prop="isHistory">
            <el-select v-model="queryParams.isHistory" placeholder="请选择是否是历史记录" id="isHistory" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_is_custom_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否能耗" prop="isEnergy">
            <el-select v-model="queryParams.isEnergy" placeholder="请选择是否是能耗记录" id="isEnergy" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_is_energy"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item label="是否报警" prop="isAlarm">
            <el-select v-model="queryParams.isAlarm" placeholder="请选择是否报警" clearable>
              <el-option
                v-for="dict in dict.type.sys_is_alarm"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>-->
          <el-form-item label="报警状态" prop="tagAlarmState">
            <el-select v-model="queryParams.tagAlarmState" placeholder="请选择报警状态" id="tagAlarmState" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_tag_alarm_state"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
<!--          <el-form-item label="报警等级" prop="alarmLevel">
            <el-select v-model="queryParams.alarmLevel" placeholder="报警等级(仅布尔变量)" clearable>
              <el-option
                v-for="alarmLevel in alarmLevelOptions"
                :key="alarmLevel.alarmtypelevel"
                :label="alarmLevel.alarmtypeExpain"
                :value="alarmLevel.alarmtypelevel"
              ></el-option>
            </el-select>
          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-d-arrow-left"
              size="mini"
              @click="handleShowLeft"
            >隐藏/显示左侧选择树</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:reg:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:reg:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:reg:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:reg:import']"
            >导入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:reg:export']"
            >导出</el-button>
          </el-col>
          <span> 项目的设备属性变量，所作修改直接影响项目弹窗的基本参数的显示</span>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="regList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="变量ID" align="center" prop="regId" width="60"/>
          <el-table-column label="设备" align="center" prop="drName" width="150" :show-overflow-tooltip="true"/>
          <el-table-column label="变量标识" align="center" prop="tagName" width="155" :show-overflow-tooltip="true"/>
          <el-table-column label="变量名称" align="center" prop="regName" width="150" :show-overflow-tooltip="true"/>
          <el-table-column label="英文名称" align="center" prop="regNameen" :show-overflow-tooltip="true"/>
          <el-table-column label="当前值" align="center" prop="tagValue" />
          <el-table-column label="单位" align="center" prop="regUnits" width="60"/>
          <el-table-column label="数据回显" align="center" prop="subName" width="80" :show-overflow-tooltip="true"/>
          <el-table-column label="值类型" align="center" prop="regType" width="110">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_value_type" :value="scope.row.regType"/>
            </template>
          </el-table-column>
          <el-table-column label="读写" align="center" prop="regReadwrite" width="50">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_read_type" :value="scope.row.regReadwrite"/>
            </template>
          </el-table-column>
          <el-table-column label="保存周期" align="center" prop="tagTime" width="80" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_data_save_time" :value="scope.row.tagTime"/>
            </template>
          </el-table-column>
          <el-table-column label="分组" align="center" prop="regGroupName" width="80" :show-overflow-tooltip="true"/>
          <el-table-column label="排序" align="center" prop="regListshowlevel" width="50"/>
          <el-table-column label="点位类型" align="center" prop="regDrshowtype" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_reg_dr_show_type" :value="scope.row.regDrshowtype"/>
            </template>
          </el-table-column>

          <!--      <el-table-column label="${comment}" align="center" prop="regDrsub" />-->
          <!--      <el-table-column label="公式" align="center" prop="regmath" />
                <el-table-column label="颜色" align="center" prop="regcolor" />-->
          <el-table-column label="3D展示" align="center" prop="isThreedShow" width="70">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_is_show" :value="scope.row.isThreedShow"/>
            </template>
          </el-table-column>
          <el-table-column label="是否历史" align="center" prop="isHistory">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_is_custom_type" :value="scope.row.isHistory"/>
            </template>
          </el-table-column>
          <el-table-column label="是否能耗" align="center" prop="isEnergy">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_is_energy" :value="scope.row.isEnergy"/>
            </template>
          </el-table-column>
<!--          <el-table-column label="是否报警" align="center" prop="isAlarm">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_is_alarm" :value="scope.row.isAlarm"/>
            </template>
          </el-table-column>-->
          <!--      <el-table-column label="报警类型" align="center" prop="alarmtype" />-->
          <!--      <el-table-column label="报警级别" align="center" prop="alarmLevel" />-->
          <!--      <el-table-column label="最小值" align="center" prop="valueMin" />
                <el-table-column label="最大值" align="center" prop="valueMax" />
                <el-table-column label="与或条件" align="center" prop="andOr" />-->
                <el-table-column label="报警状态" align="center" prop="tagAlarmState" :show-overflow-tooltip="true">
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_tag_alarm_state" :value="scope.row.tagAlarmState"/>
                  </template>
                </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding" width="100">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:reg:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:reg:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport + '&dbName=' + upload.dbName"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的项目设备属性数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改属性变量管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="变量标识" prop="tagName">
              <el-input v-model="form.tagName" placeholder="请输入寄存器变量标识" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="变量名称" prop="regName">
              <el-input v-model="form.regName" placeholder="请输入变量名称（中文）" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="当前值" prop="tagValue">
              <el-input v-model="form.tagValue" placeholder="请输入寄存器当前值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文名称" prop="regNameen">
              <el-input v-model="form.regNameen" placeholder="请输入变量名称（英文）" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="regUnits">
              <el-input v-model="form.regUnits" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据回显" prop="regSub" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.regSub" placeholder="请选择数据回显" clearable>
                <el-option
                  v-for="item in subOptions"
                  :key="item.subid"
                  :label="item.subname"
                  :value="String(item.subid)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="值类型" prop="regType">
              <el-select v-model="form.regType" placeholder="请选择变量值类型">
                <el-option
                  v-for="dict in dict.type.sys_value_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="读写类型" prop="regReadwrite">
              <el-select v-model="form.regReadwrite" placeholder="请选择设备属性读写类型">
                <el-option
                  v-for="dict in dict.type.sys_read_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保存周期" prop="tagTime">
              <el-select v-model="form.tagTime" placeholder="请选择变量保存周期">
                <el-option
                  v-for="dict in dict.type.sys_data_save_time"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位类型" prop="regDrshowtype">
              <el-select v-model="form.regDrshowtype" placeholder="请选择设备属性点位类型">
                <el-option
                  v-for="dict in dict.type.sys_reg_dr_show_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="变量分组" prop="regGroupId" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.regGroupId" placeholder="变量分组" clearable>
                <el-option
                  v-for="group in groupOptions"
                  :key="group.id"
                  :label="group.groupName"
                  :value="group.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组内排序" prop="regListshowlevel">
              <el-input v-model="form.regListshowlevel" placeholder="请输入组内顺序" style="width: 217px"/>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>
          <el-col :span="12">
            <el-form-item label="是否报警" prop="isAlarm">
              <el-select v-model="form.isAlarm" placeholder="请选择是否报警">
                <el-option
                  v-for="dict in dict.type.sys_is_alarm"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否历史" prop="isHistory">
              <el-select v-model="form.isHistory" placeholder="请选择是否是历史记录">
                <el-option
                  v-for="dict in dict.type.sys_is_custom_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否能耗" prop="isEnergy">
              <el-select v-model="form.isEnergy" placeholder="请选择是否是能耗记录">
                <el-option
                  v-for="dict in dict.type.sys_is_energy"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="3D展示" prop="isThreedShow">
              <el-select v-model="form.isThreedShow" placeholder="请选择是否3D展示">
                <el-option
                  v-for="dict in dict.type.sys_is_show"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
<!--            <el-form-item label="报警等级" prop="alarmLevel" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.alarmLevel" placeholder="报警等级(仅布尔变量)" clearable>
                <el-option
                  v-for="alarmLevel in alarmLevelOptions"
                  :key="alarmLevel.alarmtypelevel"
                  :label="alarmLevel.alarmtypeExpain"
                  :value="alarmLevel.alarmtypelevel"
                ></el-option>
              </el-select>(仅布尔变量的值类型)
            </el-form-item>-->
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警状态" prop="tagAlarmState">
              <el-select v-model="form.tagAlarmState" placeholder="请选择报警状态">
                <el-option
                  v-for="dict in dict.type.sys_tag_alarm_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-form-item label="${comment}" prop="regDrsub">
          <el-input v-model="form.regDrsub" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="公式" prop="regmath">
          <el-input v-model="form.regmath" placeholder="请输入公式" />
        </el-form-item>
        <el-form-item label="颜色" prop="regcolor">
          <el-input v-model="form.regcolor" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="最小值" prop="valueMin">
          <el-input v-model="form.valueMin" placeholder="请输入最小值" />
        </el-form-item>
        <el-form-item label="最大值" prop="valueMax">
          <el-input v-model="form.valueMax" placeholder="请输入最大值" />
        </el-form-item>
        <el-form-item label="与或条件" prop="andOr">
          <el-input v-model="form.andOr" placeholder="请输入与或条件" />
        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReg, getReg, delReg, addReg, updateReg,listDevice,alarmLevelSelect,deviceTypeTreeSelect,groupSelect,getSubOptionSelect,getDbName } from "@/api/system/reg";
import {getToken} from "@/utils/auth";

export default {
  name: "Reg",
  dicts: ['sys_reg_dr_show_type', 'sys_is_custom_type', 'sys_is_show', 'sys_is_energy', 'sys_read_type', 'sys_data_save_time', 'sys_is_alarm', 'sys_value_type', 'sys_tag_alarm_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      //设备列表使用
      subLoading: false,
      deviceLists: [],
      tableHeight: window.innerHeight - 200,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showLeft: true,
      spanColLeft: 3,
      spanColRight: 18,
      // 总条数
      total: 0,
      // 属性变量管理表格数据
      regList: [],
      //变量分组
      groupOptions: undefined,
      //报警等级
      alarmLevelOptions: undefined,
      // 设备类型选项
      deviceTypeOptions: undefined,
      // 设备属性回显定义列表
      subOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 设备类型名称
      drtypename: undefined,
      defaultDrtypeid: "",
      defaultDrId: "",
      defaultDrName: "",
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        dbName : getDbName(),
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/reg/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drtypeid: null,
        drId: null,
        drName: null,
        tagName: null,
        tagTime: null,
        regName: null,
        regNameen: null,
        regType: null,
        regUnits: null,
        regReadwrite: null,
        regGroupId: null,
        regDrshowtype: null,
        regSub: null,
        isThreedShow: null,
        isHistory: null,
        isEnergy: null,
        isAlarm: null,
        alarmLevel: null,
        tagAlarmState: null
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    drtypename(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    const drTypeId = this.$route.params && this.$route.params.drTypeId;
    const drId = this.$route.params && this.$route.params.drId;
    const drName = this.$route.params && this.$route.params.drName;
    if(drTypeId !== undefined && drId !== undefined){
      let newDrName = null;
      if(drName != null && drName.includes('|')){
        newDrName = drName.replaceAll('|','#');
      }else{
        newDrName = drName;
      }
      this.setSearchDrId(parseInt(drTypeId),parseInt(drId),newDrName);
    }
    this.getList();
    this.getGroupSelect();
    this.getAlarmLevelSelect();
    this.getDeviceTypeTree();
    this.getSubList();
  },
  methods: {
    /** 查询属性变量管理列表 */
    getList() {
      this.loading = true;
      listReg(this.queryParams).then(response => {
        this.regList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询数据回显列表 */
    getSubList() {
      getSubOptionSelect().then(response => {
        this.subOptions = response.data;
      });
    },
    /** 查询报警等级类型 */
    getAlarmLevelSelect() {
      alarmLevelSelect().then(response => {
        this.alarmLevelOptions = response.data;
      });
    },
    /** 查询字典类型详细 */
    setSearchDrId(drTypeId,drId,drName) {
      this.queryParams.drtypeid = drTypeId;
      this.queryParams.drId = drId;
      this.queryParams.drName = drName;
      this.defaultDrtypeid = drTypeId;
      this.defaultDrId = drId;
      this.defaultDrName = drName;
    },
    /** 查询变量分组 */
    getGroupSelect() {
      groupSelect().then(response => {
        this.groupOptions = response.data;
      });
    },
    /** 查询设备类型下拉树结构，左侧展示 */
    getDeviceTypeTree() {
      deviceTypeTreeSelect().then(response => {
        this.deviceTypeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    /** 查询设备列表 */
    handleNodeClick(data) {
      this.queryParams.drtypeid = data.id;
      this.queryParams.drId = null;
      this.queryParams.drName = null;
      this.handleQuery();
      this.subLoading = true;
      listDevice(this.queryParams.drtypeid).then(response => {
        this.deviceLists = response.data;
        this.subLoading = false;
      });
    },
    /** 键名前缀去除 */
    keyFormatter(cacheKey) {
      return cacheKey.replace(this.nowCacheName, "");
    },
    /** 查询设备属性列表 */
    handleDeviceRegList(row) {
      this.queryParams.drId = row.drid;
      this.queryParams.drName = row.drname;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        regId: null,
        drId: null,
        drtypeid: null,
        tagName: null,
        tagValue: null,
        tagTime: null,
        regName: null,
        regNameen: null,
        regType: null,
        regUnits: null,
        regReadwrite: null,
        regGroupId: null,
        regListshowlevel: null,
        regDrshowtype: null,
        regSub: null,
        regDrsub: null,
        regmath: null,
        regcolor: null,
        isThreedShow: null,
        isHistory: null,
        isEnergy: null,
        isAlarm: null,
        alarmtype: null,
        alarmLevel: null,
        valueMin: null,
        valueMax: null,
        andOr: null,
        tagAlarmState: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      if(this.defaultDrtypeid !== undefined && this.defaultDrId !== undefined){
        this.queryParams.drtypeid = this.defaultDrtypeid;
        this.queryParams.drId = this.defaultDrId;
        this.queryParams.drName = this.defaultDrName;
      }else{
        this.queryParams.drtypeid = null;
        this.queryParams.drId = null;
        this.queryParams.drName = null;
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.regId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleShowLeft(){
      const show = this.showLeft;
      if(show){
        this.showLeft = false;
        this.spanColLeft = 0;
        this.spanColRight = 24;
      }else{
        this.showLeft = true;
        this.spanColLeft = 3;
        this.spanColRight = 18;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加属性变量管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const regId = row.regId || this.ids
      getReg(regId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改属性变量管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.regId != null) {
            updateReg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const regIds = row.regId || this.ids;
      this.$modal.confirm('是否确认删除属性变量管理编号为"' + regIds + '"的数据项？').then(function() {
        return delReg(regIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reg/export', {
        ...this.queryParams
      }, `设备属性展示_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "项目设备属性数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/reg/importTemplate', {
      }, `reg_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;width:600px;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true,customClass: 'el-message-box' });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
<style type="text/css">
.el-message-box {
  width: 600px; /* 设置弹框宽度为视口宽度的一半 */
}
</style>
